package Operation;

import controller.DataAccessHandler;
import model.pojo.Account;

import java.util.Scanner;

/**
 * 删除账户
 */
public class DeleteAccountIOperation implements IOperation {
    @Override
    public void operate() {
        System.out.println("<删除指定账户>");
        //输入要删除的账户的ID
        Scanner input = new Scanner(System.in);
        System.out.print("输入账户ID: ");
        String accountId = input.next();
        //查询该账号是否存在
        String sql = "select account_id as accountId from tb_accounts where account_id = ?";
        Account account = DataAccessHandler.queryOne(Account.class, sql, accountId);
        if(account == null){
            System.out.println("Account doesn't exist!");
            return;
        }
        //确认删除
        System.out.println("确认删除? (Y/N)");
        if("Y".equalsIgnoreCase(input.next())){
            sql = "delete from tb_accounts where account_id = " + accountId;
            boolean isSuccess = DataAccessHandler.update(sql);
            if (isSuccess){
                System.out.println("Deleted successfully!");
            }else{
                System.out.println("Deletion failed!");
            }
        }else{
            System.out.println("Deletion canceled.");
        }
    }
}
